/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CustomerEntity;
import ejb.CustomerEntityFacade;
import ejb.CustomerOrderEntity;
import ejb.CustomerOrderEntityFacade;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author mohan
 */
public class AppOptions {
    CustomerOrderEntityFacade customerOrderEntityFacade = lookupCustomerOrderEntityFacadeBean();
    
    CustomerEntityFacade customerEntityFacade = lookupCustomerEntityFacadeBean();
    
    
    public AppOptions()
    {
        
    }
    
    //Method used to find and return the customer object for a customer ID
    public CustomerEntity getCustomer(Long CusID)
    {
        List customer = customerEntityFacade.findAll();
        
        for(Iterator it = customer.iterator(); it.hasNext();)
        {
            CustomerEntity elem = (CustomerEntity) it.next();
            if(elem.getCustomerid().equals(CusID))
            {
                return  elem;
            }
        }
        
        return null;
    }
    
    
    //Method used to check whether there is a Customer with a specific ID
    public boolean checkCustomer(Long CusID)
    {
        List customer = customerEntityFacade.findAll();
        
        for(Iterator it = customer.iterator(); it.hasNext();)
        {
            CustomerEntity elem = (CustomerEntity) it.next();
            if(elem.getCustomerid().equals(CusID))
            {
                return  true;
            }
        }
        
        return false;
        
    }
    
    
    //Method used to check whether there is a order with a specific order no
    public boolean checkCustomerOrder(String order)
    {
        List customerOrder = customerOrderEntityFacade.findAll();
        
        for(Iterator it = customerOrder.iterator(); it.hasNext();)
        {
            CustomerOrderEntity elem = (CustomerOrderEntity) it.next();
            if(elem.getOrderno().equals(order))
            {
                return  true;
            }
        }
        
        return false;
        
    }
    
    //Method used to check whether a customer is assign for a customer order already before delete it
    public boolean checkUserOrderValidity(Long CusID)
    {
        List customerOrders = customerOrderEntityFacade.findAll();
        for (Iterator it = customerOrders.iterator(); it.hasNext();) {
                CustomerOrderEntity elem = (CustomerOrderEntity) it.next();
                
                if(elem.getCustomerid().getCustomerid().equals(CusID))
                {
                    return false;
                }
                
        }
        return true;
    }
    

    private CustomerEntityFacade lookupCustomerEntityFacadeBean() {
        try {
            Context c = new InitialContext();
            return (CustomerEntityFacade) c.lookup("java:global/CustomerApp/CustomerApp-ejb/CustomerEntityFacade!ejb.CustomerEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private CustomerOrderEntityFacade lookupCustomerOrderEntityFacadeBean() {
        try {
            Context c = new InitialContext();
            return (CustomerOrderEntityFacade) c.lookup("java:global/CustomerApp/CustomerApp-ejb/CustomerOrderEntityFacade!ejb.CustomerOrderEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
    
    
}
